package com.suguowen.sblog.config.shiro;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
    /**
     * 创建ShiroFilterFactoryBean
     */
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager")DefaultWebSecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        // 设置安全管理器
        shiroFilterFactoryBean.setSecurityManager(securityManager);

        // 添加Shiro内置过滤器
        /**
         * Shiro内置过滤器，可以实现权限相关的拦截器
         *  常用的过滤器：
         *      anon：无需认证（登录）可以访问
         *      authc：必须认证才可以访问
         *      user：如果使用rememberMe的功能可以直接访问
         *      perms：该资源必须得到资源权限才可以访问
         *      role：该资源权限必须得到角色授权才可以访问
         */
        Map<String,String> filterMap = new LinkedHashMap<String,String>();
        // 放行
        filterMap.put("/toLogin","anon");
        filterMap.put("/toRegistration","anon");
        filterMap.put("/login","anon");
        filterMap.put("/registration","anon");
        filterMap.put("/toLoginSupport","anon");
        filterMap.put("/toRegistrationSupport","anon");
        // 放行主页
        filterMap.put("/home/*","anon");
        filterMap.put("/home","anon");
        filterMap.put("/","anon");
        // 放行文章详情
        filterMap.put("/toArticle/*","anon");
        // 放行静态资源
        filterMap.put("/asserts/**","anon");
        filterMap.put("/bootstrap/**","anon");
        filterMap.put("/diy/**","anon");
        filterMap.put("/editor.md-master/**","anon");

        // 授权，管理标签，增修删文章，评论管理..
        filterMap.put("/createArticle","perms[user:manager]");
        filterMap.put("/modifyArticle/*","perms[user:manager]");
        filterMap.put("/deleArticle/*","perms[user:manager]");
        filterMap.put("/createTag","perms[user:manager]");
        filterMap.put("/tagModify","perms[user:manager]");
        filterMap.put("/deleteTag","perms[user:manager]");

        // 拦截器：评论..
        filterMap.put("/**","authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);

        // 设置未登录拦截页面
        shiroFilterFactoryBean.setLoginUrl("/toLogin");
        // 设置未授权拦截页面
        shiroFilterFactoryBean.setUnauthorizedUrl("/toNoAuth");
        return shiroFilterFactoryBean;
    }

    /**
     * 创建DefaultWebSecurityManager
     */
    @Bean(name="securityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm")UserRealm userRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        // 关联Realm
        securityManager.setRealm(userRealm);
        return securityManager;
    }

    /**
     * 创建Realm
     */
    @Bean(name = "userRealm")
    public UserRealm getRealm(){
        return new UserRealm();
    }

    /**
     * 配置ShiroDialect，用于thymeleaf和shiro标签配合使用
     */
    @Bean
    public ShiroDialect getShiroDialect(){
        return new ShiroDialect();
    }
}
